﻿Option Strict On

Imports System.Data.sqlite
Imports Sunts.DataLayer.SQLite
Imports System.IO

Public Class BackupDevice
    Public Property DeviceID As Int32
    Public Property DeviceName As String
    Public Property DevicePath As String


    Public Function Insert(ConnectionString As String) As Int32
        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "INSERT INTO BackupDevices (DeviceName, DevicePath) VALUES (@DeviceName, @DevicePath)"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@DeviceName", Me.DeviceName))
                sdl.ParameterCollection.Add(New SQLiteParameter("@DevicePath", Me.DevicePath))

                Insert = sdl.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Throw New Exception("BackupDevice: Insert()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try

    End Function
    Public Function Delete(ConnectionString As String) As Int32
        Try

            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = String.Format("DELETE FROM BackupDevices WHERE DeviceID = {0}", Me.DeviceID)

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@DeviceName", Me.DeviceName))
                sdl.ParameterCollection.Add(New SQLiteParameter("@DevicePath", Me.DevicePath))

                Delete = sdl.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Throw New Exception("BackupDevice: Delete()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try
    End Function
    Public Shared Function SelectAll(ConnectionString As String) As BackupDeviceCollection
        Dim BackupDeviceCollection As BackupDeviceCollection
        Dim BackupDevice As BackupDevice
        Dim Reader As SQLiteDataReader

        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT DeviceID, DeviceName, DevicePath FROM BackupDevices"

                BackupDeviceCollection = New BackupDeviceCollection

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    BackupDevice = New BackupDevice
                    BackupDevice.DeviceID = CInt(Reader("DeviceID"))
                    BackupDevice.DeviceName = CStr(Reader("DeviceName"))
                    BackupDevice.DevicePath = CStr(Reader("DevicePath"))
                    BackupDeviceCollection.Add(BackupDevice)
                End While
                Reader.Close()
            End Using

            SelectAll = BackupDeviceCollection

        Catch ex As Exception
            Throw New Exception("BackupDevice: Delete()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            BackupDevice = Nothing
            BackupDeviceCollection = Nothing
            Reader = Nothing
        End Try
    End Function
    Public Shared Function SelectByID(BackupDeviceID As Int32, ConnectionString As String) As BackupDevice
        Dim BackupDevice As BackupDevice
        Dim Reader As SQLiteDataReader

        Try
            BackupDevice = New BackupDevice
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT DeviceID, DeviceName, DevicePath FROM BackupDevices WHERE DeviceID = @DeviceID"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@DeviceID", BackupDeviceID))

                Reader = sdl.ExecuteDataReader
                While Reader.Read
                    BackupDevice = New BackupDevice
                    BackupDevice.DeviceID = CInt(Reader("DeviceID"))
                    BackupDevice.DeviceName = CStr(Reader("DeviceName"))
                    BackupDevice.DevicePath = CStr(Reader("DevicePath"))
                End While
                Reader.Close()
            End Using

            SelectByID = BackupDevice

        Catch ex As Exception
            Throw New Exception("BackupDevice: SelectByID()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            BackupDevice = Nothing
            Reader = Nothing
        End Try
    End Function
    Public Function IsWritable() As Boolean
        Try
            If DevicePath = Nothing Or DevicePath = String.Empty Then
                Return False
            End If
            If File.Exists(DevicePath & "\HyperVBackupTest.txt") Then
                File.Delete(DevicePath & "\HyperVBackupTest.txt")
                File.Create(DevicePath & "\HyperVBackupTest.txt").Close()
                File.Delete(DevicePath & "\HyperVBackupTest.txt")
                Return True
            Else
                File.Create(DevicePath & "\HyperVBackupTest.txt").Close()
                File.Delete(DevicePath & "\HyperVBackupTest.txt")
                Return True
            End If

            Return True

        Catch ex As UnauthorizedAccessException
            Return False
        Catch ex As DirectoryNotFoundException
            Return False
        Catch ex As Exception
            Throw New Exception("BackupDevice: IsWritable()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try
    End Function
End Class


Public Class BackupDeviceCollection
    Inherits System.Collections.ObjectModel.Collection(Of BackupDevice)
End Class